"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));

var _minSafeInteger = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/number/min-safe-integer"));

var _maxSafeInteger = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/number/max-safe-integer"));

var _startsWith = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/starts-with"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _reverse = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/reverse"));

var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));

var _decimal = _interopRequireDefault(require("decimal.js"));

/**
 * 数字解析(支持整数、浮点数、负数、科学计数法)
 *
 * @since 1.1.0
 * @category 实用函数
 * @param {number} num 要解析的数字.
 *
 * @returns { result } 结果对象.
 *
 * @typedef {object} result
 * @property {array} integerList 整数数组(个、十、百、千、万...).
 * @property {array} decimalList 小数数组(十分位，百分位、千分位...).
 * @property {boolean} isNegative 是否是负数.
 *
 * @example
 *
 * z.numberTransform(12345)
 * // => { integerList: [5,4,3,2,1], decimalList: [], isNegative: false}
 *
 * z.numberTransform(-123.456)
 * // => { integerList:[3,2,1], decimalList: [4,5,6], isNegative: true}
 *
 * z.numberTransform(-1.2345e3)
 * // => { integerList: [4,3,2,1], decimalList: [5], isNegative: true}
 *
 */
function numberTransform(num) {
  var _context, _context2;

  var result = {
    integerList: [],
    // 整数
    decimalList: [],
    // 小数
    isNegative: false // 是否是负数

  };

  if (num === null || num === undefined) {
    throw new Error('没有需要解析的数值');
  }

  if (typeof num !== 'number') {
    throw new Error('类型错误');
  }

  var curNum = new _decimal.default(num);

  if (curNum < _minSafeInteger.default || curNum > _maxSafeInteger.default) {
    throw new Error('数值超出最大/最小范围');
  }

  var numStr = String(curNum);
  result.isNegative = (0, _startsWith.default)(numStr).call(numStr, '-');

  if (result.isNegative) {
    numStr = numStr.substring(1); // 移除负号
  }

  var _numStr$split = numStr.split('.'),
      _numStr$split2 = (0, _slicedToArray2.default)(_numStr$split, 2),
      integerPart = _numStr$split2[0],
      decimalPart = _numStr$split2[1]; // 输出解析后的数组 [整数位(个、十、百...)],[小数位(十分位，百分位...)]


  result.integerList = (0, _map.default)(_context = (0, _reverse.default)(_context2 = (0, _from.default)(integerPart)).call(_context2)).call(_context, Number);

  if (decimalPart) {
    var _context3;

    result.decimalList = (0, _map.default)(_context3 = (0, _from.default)(decimalPart)).call(_context3, Number);
  }

  return result;
}

module.exports = numberTransform;